package com.zunipe.trainticketsystem.database.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zunipe.trainticketsystem.database.TrainTicketDBHelper;
import com.zunipe.trainticketsystem.database.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class OrderDao {

    private final TrainTicketDBHelper dbHelper;

    public OrderDao(Context context) {
        dbHelper = new TrainTicketDBHelper(context);
    }

    public List<Order> findAll() {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        List<Order> list = new ArrayList<>();
        try (Cursor cursor = database.rawQuery("SELECT * FROM orders", new String[]{})) {
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                Integer trainId = cursor.getInt(1);
                Integer userId = cursor.getInt(2);
                String orderNumber = cursor.getString(3);
                String time = cursor.getString(4);
                String status = cursor.getString(5);
                Double price = cursor.getDouble(6);
                Order order = new Order(id, trainId, userId, orderNumber, time, status, price);
                list.add(order);
            }
        }
        return list;
    }

    public Order findById(int selectId) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        try (Cursor cursor = database.rawQuery("SELECT * FROM orders WHERE id = ?", new String[]{String.valueOf(selectId)})) {
            if (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                Integer trainId = cursor.getInt(1);
                Integer userId = cursor.getInt(2);
                String orderNumber = cursor.getString(3);
                String time = cursor.getString(4);
                String status = cursor.getString(5);
                Double price = cursor.getDouble(6);
                return new Order(id, trainId, userId, orderNumber, time, status, price);
            }
        }
        return null;
    }

    public List<Order> findByUserId(int selectUserId) {
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        List<Order> list = new ArrayList<>();
        try (Cursor cursor = database.rawQuery("SELECT * FROM orders WHERE user_id = ?", new String[]{String.valueOf(selectUserId)})) {
            while (cursor.moveToNext()) {
                Integer id = cursor.getInt(0);
                Integer trainId = cursor.getInt(1);
                Integer userId = cursor.getInt(2);
                String orderNumber = cursor.getString(3);
                String time = cursor.getString(4);
                String status = cursor.getString(5);
                Double price = cursor.getDouble(6);
                Order order = new Order(id, trainId, userId, orderNumber, time, status, price);
                list.add(order);
            }
        }
        return list;
    }

    public void updateStatus(int id, String status) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.execSQL("UPDATE orders SET status = ? WHERE id = ?", new Object[]{status, id});
    }

    public int insert(Order order) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        database.execSQL("INSERT INTO orders(train_id, user_id, order_number, time, status, price) VALUES(?, ?, ?, ?, ?, ?)",
                new Object[]{order.getTrainId(), order.getUserId(), order.getOrderNumber(), order.getTime(), order.getStatus(), order.getPrice()});
        try (Cursor cursor = database.rawQuery("SELECT last_insert_rowid()", new String[]{})) {
            if (cursor.moveToNext()) {
                return cursor.getInt(0);
            }
        }
        return 0;
    }

    public void delete(Order order) {
        SQLiteDatabase database = dbHelper.getWritableDatabase();

    }
}
